<template>
  <div class="app-container">
    <!-- 添加或修改车辆处置对话框 -->
    <el-dialog
      title="车辆处置"
      :visible="open"
      width="600px"
      append-to-body
      @close="cancel()"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="5">
          <el-col :span="24">
            <el-form-item label="车牌号" prop="carNo">
              <el-input
                v-model="form.carNo"
                placeholder="请选择车辆"
                clearable
                :style="{ width: '95%' }"
                :readonly="true"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="showCarBox = true"
                  v-if="!this.carId"
                ></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="处置原因" prop="manageReason">
              <el-radio-group v-model="form.manageReason">
                <el-radio
                  v-for="dict in dict.type.reason_for_disposal"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="处置时间" prop="manageTime">
              <el-date-picker
                clearable
                size="small"
                v-model="form.manageTime"
                type="datetime"
                :style="{ width: '95%' }"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择处置时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="处置费用"
              prop="manageMoney"
              v-if="form.manageReason != 3"
            >
              <el-input
                v-model="form.manageMoney"
                placeholder="请输入处置费用"
                :style="{ width: '95%' }"
                @keyup.native="form.manageMoney = oninput(form.manageMoney, 2)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                :style="{ width: '95%' }"
                placeholder="请输入内容"
                :maxlength="50"
                show-word-limit
                :autosize="{ minRows: 3, maxRows: 4 }"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <select-car
      v-model="showCarBox"
      :carStatus="-1"
      @success="selectCarInfo"
    ></select-car>
  </div>
</template>

<script>
import { addManagement, updateManagement } from '@/api/car/management'
import selectCar from '../../components/selectCar.vue'
export default {
  name: 'Management',
  dicts: ['reason_for_disposal'],
  components: {
    selectCar,
  },
  props: {
    carId: {
      type: String,
      default: null,
    },
    carPlateNo: {
      type: String,
      default: null,
    },
    open: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      showCarBox: false,
      // 表单参数
      form: {
        id: null,
        carId: null,
        carNo: null,
        manageReason: 1,
        manageTime: null,
        manageMoney: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: null,
        tenantId: null,
      },
      // 表单校验
      rules: {
        carNo: [
          {
            required: true,
            message: '车牌号不能为空',
            trigger: 'change',
          },
        ],
        manageReason: [
          {
            required: true,
            message: '处置原因不能为空',
            trigger: 'change',
          },
        ],
        manageTime: [
          {
            required: true,
            message: '处置时间不能为空',
            trigger: 'change',
          },
        ],
        manageMoney: [
          {
            required: true,
            message: '处置费用不能为空',
            trigger: 'change',
          },
        ],
      },
    }
  },
  created() {
    if (this.carId) {
      this.form.carId = this.carId
    }
    if (this.carPlateNo) {
      this.form.carNo = this.carPlateNo
    }
  },
  methods: {
    selectCarInfo(car) {
      this.showCarBox = false
      if (car) {
        this.form.carId = car.carId
        this.form.carNo = car.carNo
      }
      this.$forceUpdate()
    },
    // 取消按钮
    cancel() {
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        carId: null,
        carNo: null,
        manageReason: 1,
        manageTime: null,
        manageMoney: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: null,
        tenantId: null,
      }
      this.resetForm('form')
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateManagement(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.cancel()
            })
          } else {
            addManagement(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.cancel()
            })
          }
        }
      })
    },
  },
}
</script>
